﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Phones.Client.Model;

namespace Phones.Client.Repository
{
    public static class DataLayerStatics
    {
        private static PhonesEntities _context;
        /// <summary>
        /// Общий контекст данных для всех репозиториев
        /// </summary>
        //public static PhonesEntities Context
        //{
        //    get
        //    {
        //        if (_context == null)
        //        {
        //            _context = NewContext();
        //        }
        //        return _context;
        //    }
        //}

        /// <summary>
        /// Создает новый контекст данных
        /// </summary>
        /// <returns>Новый настроенный контекст данных</returns>
        public static PhonesEntities NewContext()
        {
            PhonesEntities context =
                !string.IsNullOrEmpty(ContextConnectionString.Trim()) ?
                new PhonesEntities(ContextConnectionString) :
                    new PhonesEntities();

            context.ContextOptions.LazyLoadingEnabled = false;
            context.ContextOptions.ProxyCreationEnabled = true;
            return context;
            //return new PhonesEntities();
        }

        private static string _contextConnectionString = string.Empty;

        public static string ContextConnectionString
        {
            get { return _contextConnectionString; }
            set
            {
                bool changed = _contextConnectionString != value;
                _contextConnectionString = value;
                if (_context != null && changed) _context = null;
                if (changed) _context = NewContext();
            }
        }
    }
}
